/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdfparser.parser;

import java.sql.SQLException;
import rdfparser.parser.ParserHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import rdfparser.bean.ThreadBean;

/**
 *
 * @author Amendra Shrestha
 */
public class ThreadParser extends ParserHandler {

    private String parsedValue;
    private ThreadBean thread;
    // initializing object for inner class
    private ThreadBean.ThreadContainer tContainer;
    private boolean startParser = false;
    private boolean isParent = false;
    private boolean isContainer = false;

    /**
     * parses the XML document file prints the parse data of XML document file
     * adds parse data of XML document file to database
     *
     * @param baseDir
     * @param fileDir
     * @param fileName
     */
    public void parseThread(String fileName) throws SQLException {
        thread = new ThreadBean();
        if (parseDocument(fileName)) {

            System.out.println("\tParsed document successfully.");
            printDocument();
            addData();
        } else {
            System.out.println("\tException occured while parsing document.");
        }
    }

    /**
     * overriding the default SAX start element method parses each starting XML
     * element tag and sets the required extracted data
     *
     * @param uri
     * @param localName
     * @param elementName
     * @param attributes
     */
    @Override
    public void startElement(String uri,
            String localName,
            String elementName,
            Attributes attributes) {
        if (elementName.equalsIgnoreCase("sioc:Thread")) {
            System.out.println("ThreadElements: " + elementName);
            startParser = true;
            System.out.println(startParser);
            System.out.println("Values: " + attributes.getValue("rdf:about"));
            thread.setThread(attributes.getValue("rdf:about"));
        } else if (elementName.equalsIgnoreCase("sioc:link")) {
            thread.setLink(attributes.getValue("rdf:resource"));
        } else if (elementName.equalsIgnoreCase("sioc:Forum")) {
            isParent = true;
            thread.setParentForum(attributes.getValue("rdf:about"));
        } else if (elementName.equalsIgnoreCase("rdfs:seeAlso") && isParent) {
            thread.setParentForumSeeAlso(attributes.getValue("rdf:resource"));
            isParent = false;
        } else if (elementName.equalsIgnoreCase("sioc:container_of")) {
            isContainer = true;
            // creating an object of inner class
            tContainer = thread.new ThreadContainer();
        } else if (elementName.equalsIgnoreCase("sioc:Post") && isContainer) {
            tContainer.setPost(attributes.getValue("rdf:about"));
        } else if (elementName.equalsIgnoreCase("rdfs:seeAlso") && isContainer) {
            tContainer.setSeeAlso(attributes.getValue("rdf:resource"));
        } else if (elementName.equalsIgnoreCase("sioc:previous_by_date") && isContainer) {
            tContainer.setPrevByDate(attributes.getValue("rdf:resource"));
        } else if (elementName.equalsIgnoreCase("sioc:next_by_date") && isContainer) {
            tContainer.setNextByDate(attributes.getValue("rdf:resource"));
        }
    }

    /**
     * overriding the default SAX end element method parses each ending XML
     * element tag and sets the required extracted data
     *
     * @param namespaceURI
     * @param localName
     * @param elementName
     */
    @Override
    public void endElement(String namespaceURI,
            String localName,
            String elementName) {
        if (startParser) {
            if (elementName.equals("sioc:Thread")) {
                startParser = false;
            } else if (elementName.equalsIgnoreCase("sioc:num_views")) {
                thread.setNumViews(Integer.parseInt(parsedValue));
            } else if (elementName.equalsIgnoreCase("dc:title")) {
                thread.setTitle(parsedValue);
            } else if (elementName.equalsIgnoreCase("dcterms:created")) {
                thread.setCreated(parsedValue);
            } else if (elementName.equalsIgnoreCase("sioc:container_of")) {
                thread.getContainerList().add(tContainer);
                isContainer = false;
            }
        }

    }

    /**
     * holds extracted text data in string object
     *
     * @param ch
     * @param start
     * @param length
     * @throws SAXException
     */
    @Override
    public void characters(char[] ch,
            int start,
            int length) throws SAXException {
        parsedValue = new String(ch, start, length);
    }

    /**
     * prints parsed data of XML document
     */
    private void printDocument() {
        if (thread.getThread() != null) {
            System.out.println("\t\tThread: " + thread.getThread());
            System.out.println("\t\tLink: " + thread.getLink());
            System.out.println("\t\tNum_views: " + thread.getNumViews());
            System.out.println("\t\tTitle: " + thread.getTitle());
            System.out.println("\t\tCreated: " + thread.getCreated());
            System.out.println("\t\tForum: " + thread.getParentForum());
            System.out.println("\t\tForum SeeAlso: " + thread.getParentForumSeeAlso() + "\n");
            for (ThreadBean.ThreadContainer t : thread.getContainerList()) {
                System.out.println("\t\tPost: " + t.getPost());
                System.out.println("\t\tSee Also:" + t.getSeeAlso());
                System.out.println("\t\tPrev Date: " + t.getPrevByDate());
                System.out.println("\t\tNext Date: " + t.getNextByDate() + "\n");
            }
        } else {
            System.out.println("There is nothing to print");
        }
    }

    /**
     * adds parsed data of XML document in db
     */
    private void addData() {
        System.out.println("Start adding data....\n");
        thread.addThread();
        thread.addContainer();
        System.out.println("Successfully added data....");
    }
}
